#include "twitterAPI.h"

TwitterAPI::TwitterAPI(const std::string& login, const std::string& pass)
{
    _credentials = login + ":" + pass;
}

TwitterAPI::TwitterAPI(const std::string& creds)
{
    _credentials = creds;
}

TwitterAPI::TwitterAPI()
{
    _credentials = "defaults";
}

TwitterAPI::~TwitterAPI()
{
}

void TwitterAPI::setCredentials(std::string& creds)
{
    this->_credentials = creds;
}

const std::string& TwitterAPI::homeTimeline()
{
    this->_http.setAuth(this->_credentials);
    this->_http.httpConnection("twitter.com");
    this->_http.request("GET", "/statuses/home_timeline.json");
    this->_response = this->_http.getRequest();
    this->_http.close();
    if (!this->_response)
        throw TwitterException("HTTP Request failed.");
    if (this->_response->getCode() != "200")
        throw TwitterException("Bad HTTP response : [" + this->_response->getCode() + "] " + this->_response->getMessage());
    return this->_response->getBody();
}

const std::string& TwitterAPI::updateStatus(const std::string &status)
{
    std::map<std::string, std::string> params;

    params["status"] = status;
    this->_http.setAuth(this->_credentials);
    this->_http.httpConnection("twitter.com");
    this->_http.request("POST", "/statuses/update.json", &params);
    this->_response = this->_http.getRequest();
    this->_http.close();
    if (!this->_response)
        throw TwitterException("HTTP Request failed.");
    if (this->_response->getCode() != "200")
        throw TwitterException("Bad HTTP response : [" + this->_response->getCode() + "] " + this->_response->getMessage());
    return (this->_response->getBody());
}

const std::string& TwitterAPI::deleteStatus(const std::string& id)
{
    std::map<std::string, std::string> params;

    params["id"] = id;
    this->_http.setAuth(this->_credentials);
    this->_http.httpConnection("twitter.com");
    this->_http.request("POST", "/statuses/destroy/" + id + ".json", &params);
    this->_response = this->_http.getRequest();
    this->_http.close();
    if (!this->_response)
        throw TwitterException("HTTP Request failed.");
    if (this->_response->getCode() != "200")
        throw TwitterException("Bad HTTP response : [" + this->_response->getCode() + "] " + this->_response->getMessage());
    return (this->_response->getBody());
}

const std::string& TwitterAPI::getFollowers(const std::string& name)
{
    std::map<std::string, std::string> params;

    params["id"] = name;
    this->_http.setAuth(this->_credentials);
    this->_http.httpConnection("twitter.com");
    this->_http.request("GET", "/statuses/followers.json");
    this->_response = this->_http.getRequest();
    this->_http.close();
    if (!this->_response)
        throw TwitterException("HTTP Request failed.");
    if (this->_response->getCode() != "200")
        throw TwitterException("Bad HTTP response : [" + this->_response->getCode() + "] " + this->_response->getMessage());
    return this->_response->getBody();
}

const std::string& TwitterAPI::getFollowing(const std::string& name)
{
    std::map<std::string, std::string> params;

    params["id"] = name;
    this->_http.setAuth(this->_credentials);
    this->_http.httpConnection("twitter.com");
    this->_http.request("GET", "/statuses/friends.json");
    this->_response = this->_http.getRequest();
    this->_http.close();
    if (!this->_response)
        throw TwitterException("HTTP Request failed.");
    if (this->_response->getCode() != "200")
        throw TwitterException("Bad HTTP response : [" + this->_response->getCode() + "] " + this->_response->getMessage());
    return this->_response->getBody();
}
